const fs = require('fs');

const maxParams = 10;
const host = '长沙零一互动';
const version = '1.0.0';
const title = `/**v: \n${version} \n2020年9月18日 by ${host}\n*/\n`;

/**
 * @param {string} str
 * */
const allReplace = function (str) {
    return new RegExp(str, 'g');
}

class bodyClass {
    constructor(apiName, apiBodies) {
        this.apiName = apiName;
        this.apiBodies = apiBodies;
        this.onParseBody();
    }

    onParseBody() {
        let body = this.apiBodies[0];
        console.log('----------------');
        console.log(this.apiName);
        console.log('----------------');

        let centerBody = body.split(`${this.apiName}(`)[1];
        centerBody = centerBody.split(');')[0];
        //
        console.log('转换前\n', centerBody);
        //将()=>{} 转移成 ()=>void
        centerBody = centerBody.replace(allReplace('{\n' + '    \n' + '  }'), 'void');
        centerBody = centerBody.replace(allReplace('{\n    \n}'), 'void');
        centerBody = centerBody.trim();
        for (let i = 0; i < maxParams; i++) {
            if (centerBody == '$1') {
                centerBody = '';
                break;
            }
            //参数有$n 或 n n为数字
            centerBody = centerBody.replace(`'$${i}'`, 'any');
            centerBody = centerBody.replace(`${i}`, 'any');
        }
        centerBody = centerBody.replace('(res) => void', 'Function');
        //有默认参数
        centerBody = centerBody.replace(', any', ',arg:any');
        //有对象参数
        centerBody = centerBody.replace('{', 'args:{');
        //官方提供插件不统一 或不完整
        console.log('转换后\n', centerBody);
        if (centerBody == '') centerBody = 'arg?:any';
        console.log('转换后\n', centerBody);
        this.apiBodies = centerBody;
        //
    }

    /**
     * @return string
     * */
    getBodyDts() {
        return this.apiBodies;
    }
}

class apiClass {
    constructor(head, apiName, apiBodies, apiDescription) {
        this.head = head;
        this.apiName = apiName;
        this.apiBodies = new bodyClass(apiName, apiBodies);
        this.apiDescription = apiDescription;
    }
}

async function main() {
    let json = fs.readFileSync('./src-json/type_api.json');
    let data = JSON.parse(json.toString());
    //
    let head = '';
    let apis = [];
    for (let k in data) {
        let dataDetail = data[k];
        const {prefix, body, description} = dataDetail;
        //名称
        let ary = prefix.split('.');
        if (head == '') {
            head = ary[0];
        }
        apis.push(new apiClass(head, ary[1], body, description));
    }
    //
    let dtsHead = `${title} declare namespace ${head}{\n`
    let dtsCenter = ``;
    let dtsTail = `\n}`;
    for (let api of apis) {
        dtsCenter += '/**\n' +
            '* @function' + api.apiDescription + '\n' +
            '*/\n';
        const exportFunction = `export function ${api.apiName}(\n${
            api.apiBodies.getBodyDts()
        }\n);`;
        dtsCenter += exportFunction;
        dtsCenter += '\n';
    }
    const dts = dtsHead + dtsCenter + dtsTail;
    fs.writeFileSync('./out.d.ts', dts);
}

main().then((info) => {
    console.log('ok', info || '');
}).catch((r) => {
    console.error('error', r);
})












